<?php
/**

 //  DoYouHaoBaby-X Ucenter ( DoYouHaoBaby-X 计划 )
 //  +---------------------------------------------------------------------
 //
 //  “Copyright”
 //  +---------------------------------------------------------------------
 //	 | (C) 2010 - 2011 http://doyouhaobaby.net All rights reserved.
 //	 | This is not a free software, use is subject to license terms
 //  +---------------------------------------------------------------------
 //
 //  “About This File”
 //  +---------------------------------------------------------------------
 //	 | Ucenter 角色控制器
 //  +---------------------------------------------------------------------

*/

!defined( 'DYHB_PATH' ) && exit;

class RoleController extends InitController{


   /**
    * 用户数据查询过滤
    */
   public function filter_( &$arrMap ){

		$arrMap['role_name'] = array('like',"%".Safe::getGpc( 'role_name','P' )."%");
   }

   /**
    * 选择父角色
    */
   public function select( ){

   	  $arrMap = array();
   	  $nParentid = Safe::getGpc( 'role_parentid' );

   	  if( $nParentid!='' ){

   	  	  $arrMap['role_parentid'] = $nParentid;
   	  }

   	  // 获取数据列表
	  $arrList =   RoleModel::F( $arrMap )
			       ->setColumns( 'role_id,role_name' )
			       ->all()
			       ->asArray()
			       ->query();

   	  $this->assign('arrList',$arrList );
   	  $this->display( );
   }

   /**
    * 设置项目应用授权前置处理
    */
    public function bSet_app_( ){

        $nGroupId	= Safe::getGpc( 'group_id' );
    	if( empty( $nGroupId ) ){

    		$this->E( G::L( '你没有选择分组!' ) );
    	}
    }

    /**
     * 设置应用授权
     */
    public function set_app(){

        $nId     = Safe::getGpc( 'groupAppId' );
		$nGroupId	= Safe::getGpc( 'group_id' );

		$oRoleModel = RoleModel::F()->query();
		$oRoleModel->delGroupApp( $nGroupId );
		$bResult = $oRoleModel->setGroupApps( $nGroupId,$nId );

		if($bResult===false) {

			$this->E( G::L( '项目授权失败！' ) );
		}

		else {

			$this->S( G::L( '项目授权成功！') );
		}
    }

    /**
     * 获取应用列表
     */
   public function app(){

        // 读取系统的项目列表
   	    $arrAppList=array();

        $arrList = NodeModel::F( 'node_level=?',1 )
                   ->setColumns( 'node_id,node_title' )
                   ->asArray()
                   ->all()
                   ->query();

		foreach ( $arrList as $arrVo ){

			$arrAppList[ $arrVo['node_id'] ] = $arrVo['node_title'];
		}

		// 读取系统组列表
		$arrGroupList=array();

        $arrList       =  RoleModel::F()->setColumns('role_id,role_name')
                          ->asArray()
                          ->all()
                          ->query();

		foreach ( $arrList as $arrVo){

			$arrGroupList[$arrVo['role_id']]	=	$arrVo['role_name'];
		}

		$this->assign( "arrGroupList",$arrGroupList);

        // 获取当前用户组项目权限信息
        $nGroupId =  Safe::getGpc( 'group_id' );
        if( $nGroupId===null )  $nGroupId =0;
        $this->assign( 'nGroupId',$nGroupId );

		$arrGroupAppList = array();

        $this->assign("nSelectGroupId", $nGroupId );

		if( !empty( $nGroupId ) ) {

			// 获取当前组的操作权限列表
            $arrList = RoleModel::F()->query()->getGroupAppList( $nGroupId );
			foreach ($arrList as $arrVo ){

				$arrGroupAppList[]	=	$arrVo['node_id'];
			}
		}

		$this->assign( 'arrGroupAppList',$arrGroupAppList );
        $this->assign( 'arrAppList',$arrAppList );
        $this->display();
   }

   /**
    * 获取模型列表
    */
    public function module( ){

        $nGroupId =  Safe::getGpc( 'group_id' );
        $nAppId  = Safe::getGpc( 'app_id' );

        if( $nGroupId===null ) $nGroupId =0;

        $this->assign( 'nGroupId',$nGroupId );

        // 读取系统组列表
		$arrGroupList=array();

        $arrList = RoleModel::F()
                   ->setColumns( 'role_id,role_name' )
                   ->all()
                   ->asArray()
                   ->query();

		foreach ( $arrList as $arrVo ){

			$arrGroupList[ $arrVo['role_id'] ] = $arrVo['role_name'];
		}

		$this->assign("arrGroupList",$arrGroupList);


        // 读取系统组的授权项目列表
		$arrAppList = array( );
		$this->assign( "nSelectGroupId",$nGroupId );
        if( !empty( $nGroupId ) ) {

            $arrList	=	RoleModel::F()
                            ->query()
                            ->getGroupAppList( $nGroupId );

			foreach ($arrList as $arrVo){

				$arrAppList[$arrVo['node_id']]	=	$arrVo['node_title'];
			}

        }

        $this->assign("arrAppList",$arrAppList );

        // 读取当前项目的模块列表
        $arrModuleList=array();
        $this->assign("nSelectAppId",$nAppId );
        if(!empty( $nAppId ) ) {

			$arrWhere['node_level']=2;

			$arrWhere['node_parentid'] = $nAppId;

            $arrNodelist =  NodeModel::F()
                            ->setColumns( 'node_id,node_title' )
                            ->where( $arrWhere )
                            ->asArray()
                            ->all()
                            ->query();

			foreach ( $arrNodelist as $arrVo){

				$arrModuleList[ $arrVo['node_id'] ]	= $arrVo['node_title'];
			}
        }

        // 获取当前项目的授权模块信息
		$arrGroupModuleList = array();
		if( !empty($nGroupId) && !empty( $nAppId ) ) {

            $arrGrouplist	=	RoleModel::F()
                                ->query()
                                ->getGroupModuleList( $nGroupId,$nAppId );

			foreach ( $arrGrouplist as $arrVo ){

				$arrGroupModuleList[] = $arrVo['node_id'];
			}
		}

		$this->assign( 'arrGroupModuleList',$arrGroupModuleList);
        $this->assign( 'arrModuleList',$arrModuleList);
        $this->display( );
    }

    /**
     * 设置模块授权执行前处理
     */
    public function bSet_module_(){

    	$nGroupId =  Safe::getGpc( 'group_id' );
        $nAppId  = Safe::getGpc( 'appId' );

    	if( empty( $nGroupId ) ){

    		$this->E( G::L( '你没有选择分组!') );
    	}

        if( empty( $nAppId ) ){

    		$this->E( G::L( '你没有选择APP' ) );
    	}
    }

    /**
     * 设置模块权限处理
     */
    public function set_module(){

		$nId =  Safe::getGpc( 'groupModuleId' );
        $nGroupId  = Safe::getGpc( 'group_id' );
        $nAppId	=	Safe::getGpc( 'appId' );

		RoleModel::F()
		           ->query()
		           ->delGroupModule( $nGroupId,$nAppId );

		$bResult = RoleModel::F()
		                     ->query()
		                     ->setGroupModules( $nGroupId,$nId );

		if( $bResult===false ) {

			$this->E( G::L( '模块授权失败！' ) );
		}

		else {

			$this->S( G::L( '模块授权成功！' ) );
		}
    }

    /**
     * 获取方法
     */
     public function action(){

        $nGroupId =  Safe::getGpc( 'group_id','G' );
        $nAppId  = Safe::getGpc( 'app_id','G' );
        $nModuleId  = Safe::getGpc( 'module_id','G' );

        if( $nGroupId===null ) $nGroupId =0;
        $this->assign( 'nGroupId',$nGroupId );

        if( $nAppId===null ) $nAppId =0;
        $this->assign( 'nAppId',$nAppId );

        // 读取系统组列表
        $arrGrouplist = RoleModel::F()
                        ->setColumns( 'role_id,role_name' )
                        ->asArray()
                        ->all()
                        ->query();

		foreach ($arrGrouplist as $arrVo){

			$arrGroupList[ $arrVo['role_id'] ] = $arrVo['role_name'];
		}

		$this->assign( "arrGroupList" ,$arrGroupList);

        $this->assign("nSelectGroupId",$nGroupId );
        $arrAppList =array();
        if( !empty( $nGroupId ) ) {

            // 读取系统组的授权项目列表
            $arrList	=	RoleModel::F()
                            ->query()
                            ->getGroupAppList( $nGroupId );

			foreach ($arrList as $arrVo){

				$arrAppList[ $arrVo['node_id'] ]	=	$arrVo['node_title'];
			}

        }
        $this->assign( "arrAppList",$arrAppList );

        $this->assign( "nSelectAppId",$nAppId );
        $arrModuleList =array();
        if( !empty( $nAppId ) ) {

        	// 读取当前项目的授权模块列表
            $arrList	=	RoleModel::F()
                            ->query()
                            ->getGroupModuleList( $nGroupId,$nAppId );

			foreach ($arrList as $arrVo){

				$arrModuleList[$arrVo['node_id']]	=	$arrVo['node_title'];
			}

        }

        $this->assign( "arrModuleList",$arrModuleList);

        $this->assign( "nSelectModuleId",$nModuleId );
        $arrActionList =array();
        if( !empty($nModuleId ) ) {

        	// 读取当前项目的操作列表
			$arrMap['node_level']=3;
			$arrMap['node_parentid'] = $nModuleId;
            $arrList	=	NodeModel::F()
                        ->setColumns( 'node_id,node_title' )
                        ->where( $arrMap )
                        ->asArray()
                        ->all()
                        ->query();

			if( $arrList ) {

				foreach ( $arrList as $arrVo){

					$arrActionList[$arrVo['node_id']]	=	$arrVo['node_title'];
				}
			}
        }

        $this->assign('arrActionList',$arrActionList);
        $arrGroupActionList =array();
        if( !empty( $nModuleId ) && !empty( $nGroupId ) ){

        	$arrGroupAction = RoleModel::F()
        	                  ->query()
        	                  ->getGroupActionList( $nGroupId,$nModuleId );

        	if( $arrGroupAction ){

        		foreach( $arrGroupAction as $arrVo){

        			$arrGroupActionList[] = $arrVo['node_id'];
        		}
        	}
        }

        $this->assign( 'arrGroupActionList',$arrGroupActionList);
        $this->display();

     }

    /**
     * 设置操作方法执行前处理
     */
    public function bSet_action_( ){

    	$nGroupId =  Safe::getGpc( 'group_id','P' );
        $nAppId  = Safe::getGpc( 'appId','P' );
        $nGroupActionId  = Safe::getGpc( 'groupActionId','P' );

    	if( empty( $nGroupId ) ){

    		$this->E( G::L( '你没有选择分组!' ) );
    	}

        if( empty( $nAppId ) ){

    		$this->E( G::L( '你没有选择APP！' ) );
    	}

    	if( empty( $nGroupActionId ) ) {

    		$this->E( G::L( '你没有选择方法！' ) );
    	}
    }

   /**
    * 设置操作方法列表
    */
    public function set_action( ){

        $nId =  Safe::getGpc( 'groupActionId','P' );
        $nModuleId  = Safe::getGpc( 'moduleId','P' );
        $nGroupId =  Safe::getGpc( 'group_id','P' );

		RoleModel::F()
		         ->query()
		         ->delGroupAction( $nGroupId,$nModuleId );

		$bResult = RoleModel::F()
		         ->query()
		         ->setGroupActions( $nGroupId,$nId );

		if( $bResult===false ) {

			$this->E( G::L( '操作授权失败！' ) );
		}

		else {

			$this->S( G::L( '操作授权成功！' ) );
		}

    }

    /**
     * 获取用户
     */
    public function user(){

        // 读取系统的用户列表
		$arrList = UserModel::F()
		           ->setColumns( 'user_id,user_name,user_nikename' )
		           ->asArray()
		           ->all()
		           ->query();

		foreach ($arrList as $arrVo){

			$arrUserList[ $arrVo['user_id'] ]	=	$arrVo['user_name'].' '.$arrVo['user_nikename'];
		}

        $arrList = RoleModel::F()
                   ->setColumns( 'role_id,role_name' )
                   ->asArray()
                   ->all()
                   ->query();

		foreach ($arrList as $arrVo){

			$arrGroupList[$arrVo['role_id']]	=	$arrVo['role_name'];
		}

		$this->assign( "arrGroupList",$arrGroupList );

        // 获取当前用户组信息
        $nGroupId =  Safe::getGpc( 'id' );
        $this->assign( 'nId',$nGroupId );

		$arrGroupUserList = array();
		$this->assign( "nSelectGroupId",$nGroupId );

		$arrGroupUserList =array();
		if( !empty( $nGroupId ) ) {

			// 获取当前组的用户列表
            $arrList	=	RoleModel::F()
                            ->query()
                            ->getGroupUserList( $nGroupId );

			foreach ( $arrList as $arrVo){

				$arrGroupUserList[]	=	$arrVo['user_id'];
			}

		}

		$this->assign('arrGroupUserList',$arrGroupUserList );
        $this->assign('arrUserList',$arrUserList);
        $this->display();

    }

    /**
     * 设置用户授权前处理
     */
    public function bSet_user_(){

    	$nGroupId =  Safe::getGpc( 'group_id','P' );

    	if( empty( $nGroupId ) ){

    		$this->E( G::L( '授权失败！') );
    	}
    }

    /**
     * 用户授权
     */
    public function set_user(){

		$nId =  Safe::getGpc( 'groupUserId','P' );
		$nGroupId =  Safe::getGpc( 'group_id','P' );

		RoleModel::F()
		         ->query()
		         ->delGroupUser( $nGroupId );

        $bResult	=	RoleModel::F()
                        ->query()
                        ->setGroupUsers( $nGroupId,$nId );

		if( $bResult===false) {

		    $this->E( G::L( '授权失败！') );
		}

		else {

			$this->S( G::L( '授权成功！' ) );
		}

    }

}
